package com.ce.cetag.tags.select;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.ce.cetag.util.MarkupWriterImpl;

/**
 *
 * 功能描述：
 *
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序
 * </p>
 *
 * @author ��˧��贺帅彪
 * @create date : 2007-8-7
 */
public class Select extends BodyTagSupport {

	private static final long serialVersionUID = 6485786591370687325L;

	private String nameId;

	private String invokeJS;

	private String defaultValue;

	private String disabled;

	private String name;

	public String getDisabled() {
		return disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public String getNameId() {
		return nameId;
	}

	public void setNameId(String name) {
		this.nameId = name;
	}

	public String getInvokeJS() {
		return invokeJS;
	}

	public void setInvokeJS(String invokeJS) {
		this.invokeJS = invokeJS;
	}

	@Override
	public int doStartTag() throws JspException {
		return super.doStartTag();
	}

	@Override
	public int doAfterBody() throws JspException {
		return super.doAfterBody();
	}

	@SuppressWarnings("unchecked")
	private void setPageAttribute() {
		List ids = (List) pageContext.getAttribute("ids");
		List defaultValue = (List) pageContext.getAttribute("defaultValue");
		ids.add(getNameId());
		defaultValue.add(getDefaultValue());
		pageContext.setAttribute("ids", ids);
		pageContext.setAttribute("defaultValue", defaultValue);
	}

	@Override
	public int doEndTag() throws JspException {
		setPageAttribute();

		String point = pageContext.getAttribute("head").toString();// group.getHead();
		int count = 1;
		if (pageContext.getAttribute("count") != null) {

			count = Integer.parseInt(pageContext.getAttribute("count")
					.toString()) + 1;

		}
		pageContext.setAttribute("count", count);

		for (int i = 1; i < count; i++) {
			point += ".point";
		}

		JspWriter out = pageContext.getOut();
		MarkupWriterImpl writer = new MarkupWriterImpl(out);

		writer.begin("select");
		if (getName() != null) {
			writer.attribute("name", getName());
		} else {
			writer.attribute("name", getNameId());
		}
		writer.attribute("id", getNameId());

		if (getDisabled() != null) {
			writer.attribute("disabled", getDisabled());
		}

		String str = "select_option_reset(this.selectedIndex-1 ," + point
				+ " ," + getNameId() + "_ids);" + getInvokeJS();

		writer.attribute("onChange", str);

		BodyContent bodyContent = getBodyContent();
		if (bodyContent != null) {
			writer.begin("option");
			writer.attribute("value", "");
			writer.print(bodyContent.getString());// writer.print(this.getMessages().getMessage("pleaseSelect"));
			writer.end();
			writer.end();
			pageContext.setAttribute("haveContent", new Integer(0));
		} else {
			pageContext.setAttribute("haveContent", new Integer(1));
			writer.end();
		}
		return super.doEndTag();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
